package com.yhq.spring.websocket.service.impl;

import com.yhq.spring.websocket.parser.HK2TextHandler;
import com.yhq.spring.websocket.service.STVoiceParserService;
import com.yhq.spring.websocket.util.STAuthenticationTokenUtils;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.concurrent.ListenableFuture;
import org.springframework.web.socket.WebSocketHttpHeaders;
import org.springframework.web.socket.WebSocketSession;
import org.springframework.web.socket.client.WebSocketClient;

import java.net.URI;

/**
 * @author hqyin
 * @date 2024/11/28 10:29 AM
 */
@Component
public class STVoiceParserServiceImpl implements STVoiceParserService {
    @Autowired
    private WebSocketClient webSocketClient;
    //TODO：后续可以放在配置文件中
    private static String ak = "54D024C60B864E8794F7569A5C82C22F"; // 填写您的ak
    //TODO：后续可以放在配置文件中
    private static String sk = "AF291D609F674B51BA44ACEC52654433"; // 填写您的sk
    //商汤粤语转中文地址 TODO：后续可以放在配置文件中
    private static String stHk2TextUrl = "wss://api.sensenova.cn/v1/audio/internal/transcriptions/yue/non-streaming";

    @SneakyThrows
    @Override
    public String parseVoice(String path) {
        //创建websocket连接
        WebSocketHttpHeaders headers = new WebSocketHttpHeaders();
        headers.add("Authorization", STAuthenticationTokenUtils.sign(ak, sk));
        HK2TextHandler handler = new HK2TextHandler(path);
        webSocketClient.doHandshake(handler, headers, new URI(stHk2TextUrl));
        //获取语音结果
        return handler.getParserResult();
    }
}
